package com.platform.web.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.platform.common.constants.UserConstants;
import com.platform.common.redis.RedisUtil;
import com.platform.common.redis.RedisUtilByTemp;
import com.platform.common.utils.BackJsonBeanUtil;
import com.platform.web.model.SysUser;
import com.platform.web.service.IUserService;
import com.platform.web.service.SysUserService;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by 23609 on 2018/6/7.
 */
@RestController
public class HelloController {
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
    @Resource
    IUserService userService;
    @Resource
    SysUserService sysUserService;
    @Autowired
    private MsgProducer msgProducer;
    @RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
    public String say(){
        return "hi you!!!";
    }
    //访问/hello或者/hi任何一个地址，都会返回一样的结果
    @RequestMapping(value = {"/user"})
    public Object saveUser(HttpServletRequest request, HttpServletResponse response){
//        SysUser user = new SysUser();
//        user.setId(UUID.randomUUID().toString());
//        user.setAccount("17607580731");
//        user.setName("Leo2018");
//        user.setPassword("111111");
        String cookie = request.getHeader("Cookie");
//        SysUser user1 = (SysUser) request.getSession().getAttribute(UserConstants.SESSION_KEY);
        SysUser user = userService.selectUserById("d20e0764-102c-4652-844e-7441313b6da9");
        Page<SysUser> sysUserPage = sysUserService.selectPage(new Page<SysUser>(1, 5), null);
        SysUser user1 = sysUserService.selectOne(
                Condition.create().eq("account", "admin")
        );
        sysUserService.deleteById("d20e0764-102c-4652-844e-7441313b6da9");
        Page page = new Page(1,5);
        Map<String, Object> dataMap = userService.selectUserListByPage(page);
        return dataMap;
    }
    @RequestMapping(value = "/index")
    public Object index() {
        return "index.html";
    }

    @RequestMapping(value = "/text")
    public Object text() {
        return "index.html";
    }

    /**
     * 功能描述：微信支付回调接口
     * @param msg 支付信息
     * @param tag 消息二级分类
     * @return
     */
    @RequestMapping("/message")
    public Object order(String msg, String tag) throws MQClientException, RemotingException, MQBrokerException, InterruptedException, UnsupportedEncodingException {
        /**
         * 创建一个消息实例，包含 topic、tag 和 消息体
         */
        Message message = new Message("testTopic",tag, msg.getBytes(RemotingHelper.DEFAULT_CHARSET));
        DefaultMQProducer producer = msgProducer.getProducer();
        for (int i = 0; i < 10; i++) {
            SendResult result = msgProducer.getProducer().send(message);
            System.out.println("发送响应：MsgId:" + result.getMsgId() + "，发送状态:" + result.getSendStatus());
        }
//        SendResult result = msgProducer.getProducer().send(message);
//        System.out.println("发送响应：MsgId:" + result.getMsgId() + "，发送状态:" + result.getSendStatus());
        return BackJsonBeanUtil.getBackJsonBean(1,"发送成功","",null);
    }
}
